package com.qfedu.fmmall.service.impl;

import com.qfedu.fmmall.common.dto.CountDTO;
import com.qfedu.fmmall.common.status.ResStatus;
import com.qfedu.fmmall.common.utils.PageHelper;
import com.qfedu.fmmall.common.vo.ResultVO;
import com.qfedu.fmmall.dao.ProductCommentsMapper;
import com.qfedu.fmmall.entity.ProductComments;
import com.qfedu.fmmall.service.ProductCommentsService;
import com.qfedu.fmmall.vo.ProductCommentsVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : yanBin
 * @date :2021/12/17 11:03
 */
@Service
public class ProductCommentsServiceImpl implements ProductCommentsService {
    private static final Logger log = LoggerFactory.getLogger(ProductCommentsServiceImpl.class);

    @Autowired
    private ProductCommentsMapper productCommentsMapper;


    @Override
    public ResultVO listCommentsByProductId(String productId, int limit, int pageNum) {
        try {
            Example example = new Example(ProductComments.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo(productId);
            int count = productCommentsMapper.selectCountByExample(example);
            int pageCount = count % limit == 0 ? count / limit : count / limit + 1;
            int start = pageNum - 1 < 0 ? 0 : (pageNum - 1) * limit;
            List<ProductCommentsVO> productCommentsVOS = productCommentsMapper.selectCommentsByProductId(productId, start, limit);
            if (CollectionUtils.isEmpty(productCommentsVOS)) {
                return new ResultVO(ResStatus.NO.ordinal(), "没有评价", null);
            }
            return new ResultVO(ResStatus.OK.ordinal(), "查询评价成功", new PageHelper<ProductCommentsVO>(count, pageCount, productCommentsVOS));
        } catch (Exception e) {
            log.warn(e.getLocalizedMessage());
            return new ResultVO(ResStatus.NO.ordinal(), "查询评价错误", e.getLocalizedMessage());
        }
    }

    @Override
    public ResultVO getCommentsCountByProductId(String productId) {
        try {
            List<CountDTO> countDTOS = productCommentsMapper.selectCommentsCountByProductId(productId);
            if (CollectionUtils.isEmpty(countDTOS)) {
                return new ResultVO(ResStatus.NO.ordinal(), "没有评价统计", null);
            }
            Integer total = 0;
            Integer goodTotal = 0;
            Integer midTotal = 0;
            Integer badTotal = 0;
            for (CountDTO countDTO : countDTOS) {
                if (countDTO.getCommType() == 1) {
                    goodTotal = countDTO.getTotal();
                }
                if (countDTO.getCommType() == 0) {
                    midTotal = countDTO.getTotal();
                }
                if (countDTO.getCommType() == -1) {
                    badTotal = countDTO.getTotal();
                }
                total = total + countDTO.getTotal();
            }
            if (total == 0) {
                return new ResultVO(ResStatus.NO.ordinal(), "没有评价统计", null);
            }
            String percent = String.valueOf(Double.valueOf(goodTotal) / Double.valueOf(total) * 100);
            String percentValue = percent.substring(0, percent.lastIndexOf(".") + 3) + "%";
            Map<String, Object> map = new HashMap<>();
            map.put("total", total);
            map.put("goodTotal", goodTotal);
            map.put("midTotal", midTotal);
            map.put("badTotal", badTotal);
            map.put("percentValue", percentValue);
            return new ResultVO(ResStatus.OK.ordinal(), "评价统计查询成功", map);
        } catch (Exception e) {
            log.warn(e.getLocalizedMessage());
            return new ResultVO(ResStatus.NO.ordinal(), "评价统计查询失败", e.getLocalizedMessage());
        }
    }
}
